package cn.esale.action;

import java.util.List;

import javax.servlet.ServletException;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.esale.entity.SaleOrder;
import cn.esale.entity.SaleOrderLine;
import cn.esale.service.CustomerService;
import cn.esale.service.OrderService;
import cn.esale.service.ProductService;

public class OrderAction extends ActionSupport  {
	//OrderService
	private SaleOrder item ; 
	private SaleOrderLine line ;
	private OrderService orderService = null;
	public void setOrderService(OrderService os){
		this.orderService = os;
	}
	//ProductService
	private ProductService productService = null;
	public void setProductService(ProductService ps){
		this.productService = ps;
	}
	//
	private CustomerService customerService = null;
	public void setCustomerService(CustomerService cs){
		this.customerService = cs;
	}
	//Action方法
	public String toAdd()
			throws ServletException{
		ActionContext ac=ActionContext.getContext();
		// 候选客户列表
		List cusList = this.customerService.getAll();
		ac.put("cusList", cusList);
		return SUCCESS;
	}
	public String doAdd()
			throws Exception{
		this.orderService.add(this.getItem());
		ActionContext ac=ActionContext.getContext();
		ac.getSession().put("id", item.getOdrId());
		return "addDetail";
	}
	public String toAddDetail()
			throws ServletException{
		ActionContext ac=ActionContext.getContext();
		//根据id加载SaleOrder
		Long orderId = (Long) ac.getSession().get("id");
		this.item = this.orderService.getWithLines(orderId);
		
		//产品下拉列表
		List prodList = this.productService.getAll();
		ac.put("prodList", prodList);		
		return INPUT;
	}
	public String doAddDetail()
			throws Exception{
		this.orderService.addDetail(this.getLine());
		ActionContext ac=ActionContext.getContext();
		ac.put("id", line.getSaleOrder().getOdrId());

		return "addDetail";
	}
	public SaleOrder getItem() {
		return item;
	}
	public void setItem(SaleOrder item) {
		this.item = item;
	}
	public SaleOrderLine getLine() {
		return line;
	}
	public void setLine(SaleOrderLine line) {
		this.line = line;
	}
}
